<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 16:49:33
 * @version 1.0
 */

class RegisterAction extends HomeAction {    

	//注册
    public function index(){
        $this->assign('back',$_SERVER['HTTP_REFERER']);
		$this->display();
    }

    //保存
    public function save(){
        $obj = D('Member');
        if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {

                import("ORG.Crypt.Xxtea");
                cookie('memberID',Xxtea::encrypt($list,C('COOKIE_PREFIX')));
                $state = 'SUCCESS';
                $message = '注册成功';
                if (I('post.back')) {
                    $url = I('post.back');
                }else{
                    $url = U('Index/index');
                }                
            } else {             
                $state = '注册失败';
            }
        }else{
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    //登录
    public function login(){
        $this->assign('back',$_SERVER['HTTP_REFERER']);
    	$this->display();
    }

    //验证登录
    public function checklogin(){
    	$mobile = I('post.mobile');
        $password = I('post.password');

        $obj = M('Member');
            
        $condition['mobile']=$mobile;
        $list = $obj->where($condition)->find();
        if($list) {
            if ($list['password']!=md5($password)) {
                $state = '密码不正确';
            }
                
            //登录成功
            import("ORG.Crypt.Xxtea");
            cookie('memberID',Xxtea::encrypt($list['id'],C('COOKIE_PREFIX')));
            cookie('wxid',Xxtea::encrypt($list['wxid'],C('COOKIE_PREFIX')));

            $state = 'SUCCESS';
            $message = '登录成功';
            if (I('post.back')) {
                $url = I('post.back');
            }else{
                $url = U('Index/index');
            }     
        }else{
            $state = '手机号码不存在';
        }
        echo $this->echo_json_str($state,$message,$url);
    }

}